import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import VueRouter from 'vue-router';

Vue.use(ElementUI);
Vue.use(VueRouter);

Vue.config.productionTip = false

import login from "./components/login.vue"
import loginMain from "./components/loginMain.vue"

import caigou from "./components/admin/caigou.vue"
import yingfukuan from "./components/admin/yingfukuan.vue"
import yuanliao from "./components/admin/yuanliao.vue"
import lingliao from "./components/admin/lingliao.vue"
import chengpin from "./components/admin/chengpin.vue"
import cipin from "./components/admin/cipin.vue"

import user from "./components/admin/user.vue"
import role from "./components/admin/role.vue"
import permission from "./components/admin/permission.vue"

//axios.defaults.baseURL = 'http://localhost:10004'
//配置routes
//路径和组件的对应关系
let routes = [
	 {
	 	path: '/login',
	 	component: login
	 },
	{
		path: '/',
		component: loginMain,
		//子组件
		children: [{
				path: '/cipin',
				component: cipin
			},
			{
				path: '/chengpin',
				component: chengpin
			},
			{
				path: '/lingliao',
				component: lingliao
			},
			{
				path: '/yuanliao',
				component: yuanliao
			},
			{
				path: '/yingfukuan',
				component: yingfukuan
			},
			{
				path: '/caigou',
				component: caigou
			},
            {
				path: '/user',
				component: user
			},
			{
				path: '/role',
				component: user
			},
			{
				path: '/permission',
				component: user
			},
		]
	}
]

//创建一个路由对象
let router = new VueRouter({
	routes,
	mode: "history"
})

/* const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
	return VueRouterPush.call(this, to).catch(err => err)
}
 */
 import axios from "axios"
 //请求拦截器
 axios.interceptors.request.use(
 	function(config){
 		let token = localStorage.getItem("token");
 		config.headers.token = token;
 		return config;
 	}
 )
 
 //响应拦截器
 axios.interceptors.response.use(
 	function(res){
 		if(res.headers.newtoken){
 			localStorage.setItem("token",res.headers.newtoken)
 		}
 		return res;
 	},
 	function(error){
 		if(error.response.status==403){
 			ElementUI.Message({
 				message: "权限不足",
 				type:"warning"
 			});
 			router.push("/login")
 			localStorage.removeItem("token")
 		}
 		return error;
 	}
 )

new Vue({
	router,
	render: h => h(App),
}).$mount('#app')